package com.muli.muwai.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muli.muwai.entity.IntfReturnMoney;
import com.muli.muwai.exception.MyDefinedEnum;
import com.muli.muwai.mapper.ReturnCashMapper;
import com.muli.muwai.service.ReturnCashService;
import com.muli.muwai.util.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ReturnCashServiceImpl extends ServiceImpl<ReturnCashMapper, IntfReturnMoney> implements ReturnCashService {

    @Autowired
    ReturnCashMapper returnCashMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void returnMoney(List<String[]> returnMoney) throws Exception {
        //转化成实体类的集合
        List<IntfReturnMoney> list = getIntfReturnMonies(returnMoney);

        //以下代码为正确数据代码 测试数据请注释
        HashSet<Integer> set = new HashSet<>();
        for (IntfReturnMoney money : list) {
            //获取时间
            Date startDate = money.getReturnDate();
            //获取Calendar对象
            Calendar calendar = Calendar.getInstance();
            //将startDate转化成Calendar对象
            calendar.setTime(startDate);
            //取出月份，要加1，因为是从0开始的(!)不知道为什么Sun要这样?
            int i = calendar.get(Calendar.MONTH) + 1;
            set.add(i);
        }
        //如果月份一致则里面只有一个数据,执行删除,否侧报错
        if (set.size() == 1) {
            //删除同月份的数据
            String strip = StringUtils.strip(set.toString(), "[]");
            int anInt = Integer.parseInt(strip);
            int aa = returnCashMapper.deleteData(anInt);
            //saveBatch(list);
            System.out.println(aa);
        } else {
            System.out.println(set.toString());
            throw new Exception(MyDefinedEnum.ERROR_MONTH.getMsg());
        }

        //测试数据
        //saveBatch(list);
    }

    private List<IntfReturnMoney> getIntfReturnMonies(List<String[]> returnMoney) throws Exception {
        List<IntfReturnMoney> intfReturnMoneyList = new ArrayList<>();
        for (String[] strings : returnMoney) {
            IntfReturnMoney intfReturnMoney = new IntfReturnMoney();
            intfReturnMoney.setFiscalYear(strings[0]);
            intfReturnMoney.setProfit(strings[1]);
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yy/MM/dd");
                Date date1 = sdf.parse(strings[2]);
                Timestamp timestamp1 = new Timestamp(date1.getTime());
                intfReturnMoney.setReturnDate(timestamp1);
            } catch (ParseException e) {
                int size = intfReturnMoneyList.size() + 2;
                throw new Exception("第" + size + "行,第" + 3 + "列,请设写成yyyy/m/d格式");
            }
            intfReturnMoney.setPartCompany(strings[3]);
            intfReturnMoney.setShowRetail(strings[4]);
            intfReturnMoney.setReturnNature(strings[5]);
            //intfReturnMoney.setWinthdrawMoney(strings[6]);
            try {
                if (strings[6].equals("")) {
                    intfReturnMoney.setWinthdrawMoney(Double.valueOf(0));
                } else {
                    intfReturnMoney.setWinthdrawMoney(Double.parseDouble(strings[6]));
                }
            } catch (NumberFormatException e) {
                int size = intfReturnMoneyList.size() + 2;
                throw new Exception("第" + size + "行,第" + 7 + "列,格式错误");
            }
            intfReturnMoney.setBackCash(strings[7]);
            intfReturnMoney.setCompanyName(strings[8]);
            intfReturnMoney.setRemark(strings[9]);

            intfReturnMoneyList.add(intfReturnMoney);
        }
        return intfReturnMoneyList;
    }
}
